package com.atguigu.day03;

import org.apache.flink.api.common.serialization.SimpleStringEncoder;
import org.apache.flink.core.fs.Path;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.sink.filesystem.StreamingFileSink;
import org.apache.flink.streaming.api.functions.sink.filesystem.rollingpolicies.DefaultRollingPolicy;

import java.util.concurrent.TimeUnit;

public class Flink10_Sink_FileSInk {
    public static void main(String[] args) throws Exception {
        //获取流的执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

//        env.setParallelism(1);

        //从元素获取数据
        DataStream<String> input = env.fromElements("hello world", "hello flink");


        //将流中的数据写入文件
        StreamingFileSink<String> fileSink = StreamingFileSink
                .forRowFormat(new Path("./output"), new SimpleStringEncoder<String>())
                .withRollingPolicy(DefaultRollingPolicy.builder()
                        //文件大小达到1 GB 滚动
                        .withMaxPartSize(1024 * 1024 * 1024)
                        //15分钟滚动
                        .withRolloverInterval(TimeUnit.MINUTES.toMillis(15))
                        //5分钟没数据滚动
                        .withInactivityInterval(TimeUnit.MINUTES.toMillis(5))
                        .build())
                .build();


        input.addSink(fileSink).setParallelism(1);

        env.execute();


    }
}
